;$(function () {

    window.isInt4 = function (field, rules, i, options) {
        var thisValue = field[0].value;
        var reg = /^[1-9]\d{0,3}$/;
        if (!reg.test(thisValue)) {
            var message = "请输入四位以内的正整数！"
            return message;
        }
    }
    $(".form-table").on("click", ".delete", function(){
      $(this).parent().parent().remove();
    })

    $("#addStudyCompletion").click(function(){
        addStudyCompletionLine()
    })

    function addStudyCompletionLine(){
        var index = $("#exerciseIndex").val();
        if(index == ""){
            index = 0;
        }
        var booksHtml = '';
        $("input[name='books']").each(function(){
            var books = $(this).val().split("----");
            var optionHtml = '<option value="'+books[0]+'">'+books[1]+'</option>'
            booksHtml += optionHtml
        })
        var htmlLine = '<tr class="exerciseLine">'+
                            '<td class="col-xs-2">'+
                              '<select name="exerciseCompletionDTOS['+index+'].exerciseBookId" class="form-control">'+
                                  '<option value="" selected="selected">请选择</option>'+ booksHtml +
                             ' </select>'+
                            '</td>'+
                            '<td class="col-xs-2">'+
                                '<input type="text" class="form-control validate[required,funcCallRequired[isInt4]]" name="exerciseCompletionDTOS['+index+'].startExerciseNum"'+
                                   'value="" placeholder="请填写数字"/>'+
                            '</td>'+
                            '<td class="col-xs-2">'+
                                '<input type="text" class="form-control validate[required,funcCallRequired[isInt4]]" name="exerciseCompletionDTOS['+index+'].endExerciseNum"'+
                                   'value="" placeholder="请填写数字"/>'+
                            '</td>'+
                            '<td class="col-xs-2">'+
                                '<button type="button" class="btn btn-danger delete">删除</button>'+
                            '</td>'+
                       '</tr>';
         var table = $(".form-table")
         table.append($(htmlLine))
         $("#exerciseIndex").val(parseInt(index)+1)
    }
});